package com.study_online.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.study_online.domain.Order;
import com.study_online.domain.ResultInfo;
import com.study_online.mapper.OrderMapper;
import com.study_online.service.OrderService;
import com.study_online.utils.TimeUtils;
import com.study_online.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 张天霸
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 添加订单
     * @param order
     * @return
     */
    @Override
    public ResultInfo insert(Order order) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            //设置条件
            order.setOrderNum(UuidUtil.getUuid());
            order.setOrderStatus("未支付");
            //获得uid
//            User user = LoginInterceptor.getUser();
//            Integer uid = user.getUid();
//            order.setUid(uid);
            order.setUid(9);
            order.setAddTime(TimeUtils.getTime());
            //添加数据
            order.insert();
            Order o = orderMapper.selectById(order.getOrderId());
            info.setData(o);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("添加失败！");
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 修改订单为支付状态
     * @param orderNum
     * @param payTime
     * @param alipayNum
     * @return
     */
    @Override
    public ResultInfo updateStatus(String orderNum,String payTime,String alipayNum) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            QueryWrapper<Order> wrapper = new QueryWrapper<>();
            wrapper.eq("order_num",orderNum);
            Order order = new Order();
            order.setOrderStatus("已支付");
            order.setPayTime(payTime);
            order.setAlipayNum(alipayNum);
            orderMapper.update(order,wrapper);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("修改失败！");
            e.printStackTrace();
        }
        return null;
    }
}